package FlexTrade.Auctions;


import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Hashtable;
import java.util.Timer;
import java.util.TimerTask;

import FlexTrade.AbstractAuction;

import common_classes.AuctionPrimitiveData;
import common_classes.AuctionStates;
import common_classes.BidPriceData;
import common_classes.IAuction;
import common_classes.WSEvent;


public final class EnglishAuctionImpl extends AbstractAuction {
	/**
	 * 
	 */
	private static final long serialVersionUID = -8374014560067567340L;
	
	
	
	public EnglishAuctionImpl(String _uniqueAuctionID) {
		this.uniqueAuctionID = _uniqueAuctionID;
		auctionStates = new AuctionStates();
		eventPool = new Hashtable<String, ArrayList<WSEvent>>();
	}
	
	public EnglishAuctionImpl(){
		auctionStates = new AuctionStates();
		eventPool = new Hashtable<String, ArrayList<WSEvent>>();
	}
		
	public EnglishAuctionImpl(AuctionPrimitiveData _auctionPrimitiveData) {
		// TODO Auto-generated constructor stub
		auctionStates = new AuctionStates();
		this.auctionPrimitiveData = _auctionPrimitiveData;
		this.uniqueAuctionID = _auctionPrimitiveData.uniqueAuctionID;
		eventPool = new Hashtable<String, ArrayList<WSEvent>>();
	}

	@Override
	public boolean receiveBidPriceFromCustomer(BidPriceData _bidPriceData) 
														throws RemoteException {
		//add this PriceData to list of participants
		if(_bidPriceData.bidPrice > auctionStates.highestBidPrice){	//if new price is the highest
			//update the auction status
			auctionStates.secondHighestBidPrice = auctionStates.highestBidPrice;
			auctionStates.highestBidPrice = _bidPriceData.bidPrice;
			auctionStates.highestBidCustomer = _bidPriceData.customer;
		}
		//store the last customer that participated in the auction
		lastBidPriceData = _bidPriceData;
	
		//notify provider and other clients
		//fireNotify(IAuction.E_BID_PRICE_RECEIVED);
		putEventToPool(IAuction.E_BID_PRICE_RECEIVED);
		return true;
	}

	public void setAuctionTimeDuration(long timeInMicroSecond) {
	    Date timeToRun = new Date(System.currentTimeMillis() + timeInMicroSecond);
	    Timer timer = new Timer();
	    System.out.println("Time duration is: " + Long.toString(timeInMicroSecond));
	    timer.schedule(new TimerTask() {
	            public void run() {
	                // Task here ...
	            	//auctionStates.currentStatus = IAuction.AUCTION_FINISHED;
	            	//fireNotify(IAuction.E_AUCTION_FINISH);
	            	putEventToPool(IAuction.E_AUCTION_FINISH);
	            }
	        }, timeToRun);
	}
}
